package org.inspiration.dudu.content.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.inspiration.dudu.content.model.entity.ArticleEntity;
import org.inspiration.dudu.content.model.vo.ArticleSimpleVO;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<ArticleEntity> {
    /**
     * 创建新的文章
     * @param articleEntity 文章实体
     */
    @Insert("INSERT INTO article (id, title, content_file_path, user_id, status, description) " +
            "VALUES (#{id}, #{title}, #{contentFilePath}, #{userId}, #{status}, #{description})")
    void createArticle(ArticleEntity articleEntity);

    /**
     * 根据文章ID查找文章
     * @param id 文章ID
     * @return 文章实体
     */
    @Select("select * from article where id = #{id}")
    ArticleEntity getArticleById(String id);

    /**
     * 获取最近的文章
     * @param offset 偏移量
     * @param pageSize 页面大小
     * @return 文章列表
     */
    @Select("SELECT * FROM article where status = 'PUBLISHED' ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    List<ArticleEntity> getPublishedArticlesOrderByDESC(@Param("offset") int offset, @Param("pageSize") int pageSize);

    /**
     * 增加文章的点赞数
     * @param contentId 文章ID
     */
    @Update("UPDATE article set like_count = like_count + 1 where id = #{contentId}")
    void increaseLikeCount(String contentId);

    /**
     * 减少文章的点赞数
     * @param contentId 文章ID
     */
    @Update("UPDATE article set like_count = like_count - 1 where id = #{contentId}")
    void decreaseLikeCount(String contentId);

    /**
     * 根据文章ID获取用户ID
     * @param contentId 文章ID
     * @return 用户ID
     */
    @Select("select user_id from article where id = #{contentId}")
    Long getUserIdByContentId(String contentId);

    /**
     * 增加文章的收藏数
     * @param contentId 文章ID
     */
    @Update("update article set favorite_count = favorite_count + 1 where id = #{contentId}")
    void increaseFavoriteCount(String contentId);

    /**
     * 减少文章的收藏数
     * @param contentId 文章ID
     */
    @Update("update article set favorite_count = favorite_count - 1 where id = #{contentId}")
    void decreaseFavoriteCount(String contentId);

    /**
     * 增加文章的评论数
     * @param contentId 文章ID
     */
    @Update("update article set comment_count = comment_count + 1 where id = #{contentId}")
    void increaseCommentCount(String contentId);




    @Select("select * from article where user_id = #{userId} and status = 'PUBLISHED' order by created_at desc limit #{offset}, #{pageSize}")
    List<ArticleEntity> getPublishedArticlesByUserIdOrderByDESC(Long userId, Integer offset, Integer
            pageSize);

    @Select("select * from article where user_id = #{userId} and status = 'DRAFT' order by created_at desc limit #{offset}, #{pageSize}")
    List<ArticleEntity> getDraftArticlesByUserIdOrderByDESC(Long userId, Integer offset, Integer pageSize);

    @Update("update article set title = #{title}, description = #{description}, updated_at = CURRENT_TIMESTAMP where id = #{id} ")
    void updateArticle(ArticleEntity articleEntity);

    @Update("update article set status = #{status} where id = #{id}")
    void updateArticleStatus(String id, String status);

    @Select("SELECT * FROM article WHERE user_id = #{userId} AND (title LIKE CONCAT('%', #{keyWord}, '%') OR description LIKE CONCAT('%', #{keyWord}, '%')) ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    List<ArticleSimpleVO> getArticlesByUserIdAndKeyWordOrderByDESC(Long userId, String keyWord, Integer offset, Integer pageSize);

    @Select("select * from article where title LIKE CONCAT('%', #{keyWord}, '%') OR description LIKE CONCAT('%', #{keyWord}, '%') ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    List<ArticleSimpleVO> getPublishedArticlesByKeyWordOrderByDESC(String keyWord, Integer offset, Integer pageSize);
}
